package org.ws.task.model.protocol.base;

import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public enum RequestAction {

    PULL_TASK(0,true), // 拉取
    PUSH_TASK(1,true), // 推送
    SYNC_DATA(2,true), // 节点同步数据
    META_DATA(3,true), // 元数据
    COMMIT_STATUS(4,true), // 提交状态
    HEARTBEAT(5,false); // 心跳

    private static Logger log = LoggerFactory.getLogger(RequestAction.class.getName());

    private int code;

    private boolean answer;

    public int getCode() {
        return code;
    }

    public boolean isAnswer() {
        return answer;
    }

    private static final Map<Integer,RequestAction> requestActionMap = Maps.newHashMap();
    static {
        for (RequestAction value : RequestAction.values()) {
            requestActionMap.put(value.getCode(),value);
        }
    }

    RequestAction(int code, boolean answer) {
        this.code = code;
        this.answer = answer;
    }

    public static RequestAction valueOf(int code){
        RequestAction requestAction = requestActionMap.get(code);
        if(requestAction == null) {
            log.error("can't find action:{}", code);
        }
        return requestAction;
    }
}
